/**
 * 将指定 key 存储到本地
 * @param {*} key 
 * @param {*} data 
 */
const setStorage = (key, data) => {
  try {
    wx.setStorageSync(key, data)
  } catch (e) {
    console.error(`存储key：${key} 出现错误`, error)
  }
}

/**
 * 从本地读取指定 key
 * @param {*} key 
 */
const getStorage = (key) => {
  try {
    const data = wx.getStorageSync(key)
    if (data) {
      return data
    }
  } catch (e) {
    console.error(`读取key：${key} 出现错误`, error)
  }
}

/**
 * 从本地移除指定 key
 * @param {*} key 
 */
const removeStorage = (key) => {
  try {
    wx.removeStorageSync(key)
  } catch (e) {
    console.error(`移除key：${key} 出现错误`, error)
  }
}

/**
 * 清空本地存储
 */
const clearStorage = () => {
  try {
    wx.clearStorageSync()
  } catch (e) {
    console.error(`清空本地存储时，出现错误`, error)
  }
}

/**
 * 异步存储指定 key 到本地
 * @param {*} key 
 * @param {*} data 
 */
const asyncSetStorage = (key, data) => {
  return new Promise((resolve) => {
    wx.setStorage({
      key,
      data,
      complete(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 异步从本地获取指定 key
 * @param {*} key 
 */
const asyncGetStorage = (key) => {
  return new Promise((resolve) => {
    wx.getStorage({
      key,
      complete(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 异步从本地移除指定 key
 * @param {*} key 
 */
const asyncRemoveStorage = (key) => {
  return new Promise((resolve) => {
    wx.removeStorage({
      key,
      complete(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 异步清空本地数据
 */
const asyncClearStorage = () => {
  return new Promise((resolve) => {
    wx.clearStorage({
      complete(res) {
        resolve(res)
      }
    })
  })
}
export {
  setStorage,
  getStorage,
  removeStorage,
  clearStorage
}

export {
  asyncSetStorage,
  asyncGetStorage,
  asyncRemoveStorage,
  asyncClearStorage
}